<?xml version="1.0" encoding="UTF-8"?>
<p:declare-step xmlns:p="http://www.w3.org/ns/xproc"
                xmlns:c="http://www.w3.org/ns/xproc-step"
                name="main"
                version="1.0">

   <!--
      This pipeline "compiles" a Qizx package descriptor to an XProc library.
      Every service in a package is compiled to to a p:declare-step with a
      name derived from the service.  The step has the same options as the
      service parameters.  The step create the correct URI when called with
      some value, send the request to Qizx, then return the result.
      
      The compiled steps require a parameter port named 'qizx' with both
      parameters 'username' and 'password'.
      
      This compiler requires a parameter port named 'qizx' with the paramaters
      'username', 'password', 'domain' (the server domain) and 'port' (the
      server port number, optional).  As well as the option 'package' with the
      name of the package to compile, and the option 'lib-ns' with the URI to
      use for the library target namespace.
   -->

   <!-- The package to compile. -->
   <p:option name="package" required="true"/>
   <!-- The namespace to use for the compiled step. -->
   <p:option name="lib-ns"  required="true"/>

   <!-- Must contain: username, password, domain and port (port optional). -->
   <p:input port="qizx" kind="parameter"/>

   <!-- Return the package compiled as an XProc library. -->
   <p:output port="result"/>

   <!-- Analyze the Qizx params (credentials + location). -->
   <p:parameters name="qizx-params">
      <p:input port="parameters">
         <p:pipe port="qizx" step="main"/>
      </p:input>
   </p:parameters>

   <!-- This pipeline uses an HTTP c:request as initial input. -->
   <p:identity>
      <p:input port="source">
         <!--
            For now, Qizx does not return a Content-Type header for the package
            descriptor, so we need override-content-type; that can hide a Qizx
            error though... 
         -->
         <p:inline>
            <c:request method="get" auth-method="basic" override-content-type="application/xml"/>
         </p:inline>
      </p:input>
   </p:identity>

   <!-- We need an artificial group to have variable reference qizx-params. -->
   <p:group>

      <!-- Convenient variables bound to parameters. -->
      <p:variable name="username" select="c:param-set/c:param[@name eq 'username']/@value">
         <p:pipe port="result" step="qizx-params"/>
      </p:variable>
      <p:variable name="password" select="c:param-set/c:param[@name eq 'password']/@value">
         <p:pipe port="result" step="qizx-params"/>
      </p:variable>
      <p:variable name="domain" select="c:param-set/c:param[@name eq 'domain']/@value">
         <p:pipe port="result" step="qizx-params"/>
      </p:variable>
      <p:variable name="port" select="c:param-set/c:param[@name eq 'port']/@value">
         <p:pipe port="result" step="qizx-params"/>
      </p:variable>

      <!-- The package's URI (the location of Qizx + /xqs/ + service name). -->
      <p:variable name="package-uri" select="
          concat('http://', $domain, ':'[$port], $port, '/qizx/xqs/', $package, '/')"/>

      <!-- Add the computed attributes to the HTTP c:request element. -->
      <p:add-attribute match="c:request" attribute-name="username">
         <p:with-option name="attribute-value" select="$username"/>
      </p:add-attribute>
      <p:add-attribute match="c:request" attribute-name="password">
         <p:with-option name="attribute-value" select="$password"/>
      </p:add-attribute>
      <p:add-attribute match="c:request" attribute-name="href">
         <p:with-option name="attribute-value" select="$package-uri"/>
      </p:add-attribute>

      <!-- Actually send the request to Qizx. -->
      <p:http-request/>

      <!-- Actually compile the service descriptor to an XProc library. -->
      <p:xslt>
         <p:input port="stylesheet">
            <p:document href="todooooo.xsl"/>
         </p:input>
         <p:with-param name="package-uri" select="$package-uri"/>
         <p:with-param name="lib-ns"      select="$lib-ns"/>
      </p:xslt>

   </p:group>

</p:declare-step>
